//
//  KKJoinXuanShangVC.m
//  DoMi
//
//  Created by 康凯 on 2016/12/6.
//  Copyright © 2016年 kk. All rights reserved.
//

#import "KKJoinXuanShangVC.h"
#import "KKComHttp.h"
#import "KKQuestionDetailM.h"
#import "IQKeyboardManager.h"

@interface KKJoinXuanShangVC ()<UITextViewDelegate,UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *avatar;
@property (weak, nonatomic) IBOutlet UILabel *nickname;
@property (weak, nonatomic) IBOutlet UILabel *job;
@property (weak, nonatomic) IBOutlet UILabel *label;
@property (weak, nonatomic) IBOutlet UILabel *title1;
@property (weak, nonatomic) IBOutlet UILabel *content;
@property (weak, nonatomic) IBOutlet UILabel *money;
@property (weak, nonatomic) IBOutlet UILabel *countDownTime;
@property (weak, nonatomic) IBOutlet UILabel *joinCount;
@property (weak, nonatomic) IBOutlet UILabel *browerCount;
@property (weak, nonatomic) IBOutlet UITextView *inputContent;
@property (weak, nonatomic) IBOutlet UIButton *publishBtn;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UILabel *time;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *scrollHeight;
@property (weak, nonatomic) IBOutlet UIView *xuanShangView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *xuanShangViewDetail;


@property (strong, nonatomic)KKQuestionDetailM *questionInfo;
@property (strong, nonatomic)NSTimer *timer;
@end

@implementation KKJoinXuanShangVC{
    int countTime;
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.timer invalidate];
    [IQKeyboardManager sharedManager].enable = YES;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [IQKeyboardManager sharedManager].enable = NO;
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=@"参与悬赏";
    self.scrollHeight.constant=screenHeight-self.topHeight;
    
    self.inputContent.delegate=self;
    self.scrollView.delegate=self;
    
    [self getQuestionDetail];
    if([self.xuanShangMoney isEqualToString:@"0.00"]){
        self.xuanShangView.hidden=YES;
        self.xuanShangViewDetail.constant=0;
        return;
    }
}

-(void)initData{
    [self.avatar sd_setImageWithURL:[NSURL URLWithString:self.questionInfo.avatar] placeholderImage:[UIImage imageNamed:@"default_avater"]];
    if([KKAppUtils isEmptyString:self.questionInfo.nickname]){
        self.nickname.text=@"匿名";
    }else{
        self.nickname.text=self.questionInfo.nickname;
    }
    self.job.text=self.questionInfo.job;
    self.label.text=self.questionInfo.label;
    self.title1.text=self.questionInfo.title;
    self.content.text=self.questionInfo.questiondesc;
    self.joinCount.text=[NSString stringWithFormat:@"(%@人)已参与",self.questionInfo.answercoun];
    self.browerCount.text=[NSString stringWithFormat:@"(%@人)浏览",self.questionInfo.browser];
    self.money.text=[NSString stringWithFormat:@"%.1f元",[self.questionInfo.money floatValue]];
    self.time.text=self.questionInfo.systime;
    countTime=[self.questionInfo.lefttime intValue];
    
    self.timer=[NSTimer scheduledTimerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
        if(countTime>0){
            self.countDownTime.text=[NSString stringWithFormat:@"距悬赏结束还有%@",[KKAppUtils timeformatFromSeconds:countTime]];
            countTime--;
        }else{
            [self.timer invalidate];
            self.countDownTime.text=[NSString stringWithFormat:@"悬赏已经结束"];
        }
    }];
}

#pragma mark scrollView代理
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [self.view endEditing:YES];
}

#pragma mark textview代理
-(void)textViewDidBeginEditing:(UITextView *)textView{
    if([textView.text isEqualToString:@"请输入内容"]){
        textView.text=@"";
        textView.textColor=[UIColor blackColor];
    }
}
-(void)textViewDidEndEditing:(UITextView *)textView{
    if(textView.text.length < 1){
        textView.text = @"请输入内容";
        textView.textColor = [UIColor grayColor];
    }
}
#pragma mark 按钮--确认发布
- (IBAction)confirmClick:(id)sender {
    if([self.inputContent.text isEqualToString:@"请输入内容"]){
        [self toast:@"请输入内容"];
        return;
    }
    if([self.questionInfo.uid isEqualToString:UID]){
        [self toast:@"不能参加自己的问题"];
        return;
    }
    [self joinQuestion];
}

#pragma mark 接口--获取悬赏详情
-(void)getQuestionDetail{
    [self showDialog];
    [KKComHttp getQuestionDetail:self.questionId success:^(id data) {
        [self.timer invalidate];
        self.questionInfo=data;
        [self initData];
        [self dismissDialog];
    } failure:^(NSError *error) {
        [self.timer invalidate];
        [self dismissDialog];
    }];
}
#pragma mark 接口--参与悬赏
-(void)joinQuestion{
    NSDictionary *params=@{UID_KEY:UID,TOKEN_KEY:TOKEN,@"questionid":self.questionId,@"respondcontent":self.inputContent.text};
    [KKComHttp joinQuestion:params success:^(id data) {
        [self toast:@"参与悬赏成功"];
        self.joinXSBlock();
        [self.navigationController popViewControllerAnimated:YES];
    } failure:^(NSError *error) {
        
    }];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
@end
